// src/stores/video/voice.js
import { defineStore } from 'pinia'
import { ref, computed} from 'vue'
import {getVoiceById, getVoiceList, getVoiceStats} from '@/api/video/voice'
import { ElMessage } from 'element-plus'

export const useVoiceStore = defineStore('voice', () => {
    const voices = ref([])
    const voice = ref([])
    const voiceStats = ref({})
    const loading = ref(false)
    const total = ref(0)

    // 获取语音列表
    const fetchVoices = async (params) => {
        try {
            loading.value = true
            const { data } = await getVoiceList(params)
            voices.value = data.records
            total.value = data.total
        } catch (error) {
            ElMessage.error(error.message || '获取语音列表失败')
        } finally {
            loading.value = false
        }
    }

    // 获取统计数据
    const fetchStats = async () => {
        try {
            const { data } = await getVoiceStats()
            voiceStats.value = data
        } catch (error) {
            ElMessage.error(error.message || '获取统计数据失败')
        }
    }

    // 获取语言数据
    const fetchVoiceById = async (id) => {
        try {
            loading.value = true
            const { data } = await getVoiceById(id)
            voice.value = data
            return data
        } catch (error) {
            ElMessage.error(error.message || '获取统计数据失败')
        } finally {
            loading.value = false
        }
    }

    // 获取语言列表
    const getLanguages = computed(() => {
        const languages = new Set(voices.value.map(voice => voice.language))
        return Array.from(languages)
    })

    // 获取性别列表
    const getGenders = computed(() => {
        const genders = new Set(voices.value.map(voice => voice.gender))
        return Array.from(genders)
    })

    return {
        total,
        voices,
        voice,
        voiceStats,
        loading,
        getLanguages,
        getGenders,
        fetchVoices,
        fetchStats,
        fetchVoiceById
    }
})